var movies = new Array();

var getAllMovies = function() {
	return $.ajax({
		type : 'GET',
		url : 'api/movie',
		dataType : 'json'
	}).fail(function(jgXHR, textStatus) {
		alert(jgXHR.status + ': ' + textStatus);
	}).done(function(data, textStatus, request) {
		movies = data;
	});
};

var getAvgRatingForMovie = function(parentHeading, imdb) {
	accessToken = localStorage.getItem('access-token');
	$.ajax({
		type : 'GET',
		url : 'api/rating/movie/' + imdb,
		dataType : 'json',
		beforeSend : function(request) {
			request.setRequestHeader("access-token", accessToken);
		}
	}).fail(function(jgXHR, textStatus) {
		//Do Nothing, not logged in means no ratings!
	}).done(function(data, textStatus, request) {
		var avgRating = data.avgRating;
		if(avgRating === 0) {
			parentHeading.innerHTML = parentHeading.innerHTML + "<br><small>No avg rating yet!" + "</small>";
		} else {
			parentHeading.innerHTML = parentHeading.innerHTML + "<br><small>Avg rating: " + data.avgRating + " stars</small>";
		}
	});
};